package com.atguigu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.model.process.Process;
import com.atguigu.model.system.SysUser;
import com.atguigu.service.MessageService;
import com.atguigu.service.OaProcessService;
import com.atguigu.service.OaProcessTemplateService;
import com.atguigu.service.SysUserService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private OaProcessService oaProcessService;
    @Autowired
    private OaProcessTemplateService oaProcessTemplateService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private WxMpService wxMpService;

    @Override
    public void pushPendingMessage(Long userId, Long processId, String taskId) {
        Process process = oaProcessService.getById(processId);
        SysUser sysUser = sysUserService.getUserById(userId.intValue());
        WxMpTemplateMessage.WxMpTemplateMessageBuilder builder = WxMpTemplateMessage.builder();
        String openId = sysUser.getOpenId();
        if (StringUtils.isEmpty(openId)){
            openId="oqQM96_VF842kTQRIOlKVxb7Zyn0";
        }
        JSONObject jsonObject = JSON.parseObject(process.getFormValues());
        JSONObject formShowData = jsonObject.getJSONObject("formShowData");
        StringBuffer sb=new StringBuffer();
        for (Map.Entry<String, Object> stringObjectEntry : formShowData.entrySet()) {
            sb.append(stringObjectEntry.getKey()+":"+stringObjectEntry.getValue()+"\n");
        }
        builder.toUser(openId);
        builder.templateId("iJ0I8Cd-6HW1LEp9O4qiljZ5wHX1_F9oWTvisg94FWI");
        builder.url("http://sdps.v2.idcfengye.com/#/show/"+processId+"/"+taskId);
        WxMpTemplateMessage build = builder.build();
        build.addData(new WxMpTemplateData("first",process.getTitle()+"等待"+sysUser.getName()+"审批"))
                .addData(new WxMpTemplateData("kerword1",process.getProcessCode()))
                .addData(new WxMpTemplateData("kerword2",new DateTime(process.getCreateTime()).toString("yyyy-MM-dd HH:mm:ss")))
                .addData(new WxMpTemplateData("kerword3",sb.toString()));
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(build);
            System.out.println(msg);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

    }
    //和上面一样
    @Override
    public void pushProcessedMessage(Long id, Long userId, Integer status) {

    }
}
